package cn.eveo.refreshandloadmore.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.IntDef;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import cn.eveo.refreshandloadmore.R;

/**
 * 底部加载提示项
 */
public class LoadMoreView extends LinearLayout {

    public final static int STATE_LOADING = 0;
    public final static int STATE_COMPLETE = 1;
    public final static int STATE_NOMORE = 2;
    public final static int STATE_NOT_ENABLED = 3;
    private int state;

    private RotateAnimation mLoopAnimation;
    private LinearLayout containtLayout;
    private TextView mText;
    private View mProgressBar;

    @IntDef(flag = true, value = {STATE_LOADING, STATE_COMPLETE, STATE_NOMORE, STATE_NOT_ENABLED})
    @Retention(RetentionPolicy.SOURCE)
    public @interface FooterState {
    }

    public LoadMoreView(Context context) {
        super(context);
        initView();
    }

    public LoadMoreView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public LoadMoreView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setGravity(Gravity.CENTER);

        containtLayout = new LinearLayout(getContext());
        containtLayout.setGravity(Gravity.CENTER);
        addView(containtLayout, new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        initProgessBar();
        initTitle();
        buildAnimation();
        setState(STATE_COMPLETE);
    }

    private void initTitle() {
        int margin = dip2px(getContext(), 10);
        LayoutParams textLayoutParams = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textLayoutParams.setMargins(margin, margin, margin, margin);
        mText = new TextView(getContext());
        mText.setLayoutParams(textLayoutParams);
        mText.setTextSize(12);
        mText.setTextColor(Color.parseColor("#ff666666"));
        containtLayout.addView(mText);
    }

    private void initProgessBar() {
        LayoutParams proLayoutParams = new LayoutParams(
                dip2px(getContext(), 10), dip2px(getContext(), 10));
        proLayoutParams.gravity = Gravity.CENTER_VERTICAL;
        mProgressBar = new ImageView(getContext());
        mProgressBar.setLayoutParams(proLayoutParams);
        mProgressBar.setBackgroundResource(R.drawable.ptr_progressbar);
        containtLayout.addView(mProgressBar);
    }

    private void buildAnimation() {
        mLoopAnimation = new RotateAnimation(0, 3600, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        mLoopAnimation.setInterpolator(new LinearInterpolator());
        mLoopAnimation.setDuration(10000);
        mLoopAnimation.setRepeatCount(Animation.INFINITE);
        mLoopAnimation.setFillAfter(true);
    }

    public void setState(@FooterState int state) {
        this.state = state;
        mProgressBar.clearAnimation();

        switch (state) {
            case STATE_LOADING:
                mProgressBar.setVisibility(View.VISIBLE);
                mProgressBar.startAnimation(mLoopAnimation);
                mText.setText("正在加载...");
                containtLayout.setVisibility(View.VISIBLE);
                break;
            case STATE_COMPLETE:
                mProgressBar.setVisibility(View.GONE);
                mText.setText("查看更多");
                containtLayout.setVisibility(View.VISIBLE);
                break;
            case STATE_NOMORE:
                mText.setText("没有更多内容");
                mProgressBar.setVisibility(View.GONE);
                containtLayout.setVisibility(View.VISIBLE);
                break;
            case STATE_NOT_ENABLED:
                mText.setText("");
                mProgressBar.setVisibility(View.GONE);
                containtLayout.setVisibility(View.GONE);
                break;
        }
    }

    public int getState() {
        return state;
    }

    public int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

}
